import request, { type ListQuery, type ListResponse } from "@/utils/request";
import { type SubjectType } from "../merchant/subject";

const CATEGORY_BASE_URL = "/api";

const CategoryAPI = {
  getCategoryList: (params: CategoryListParams) => {
    return request.get<any, ListResponse<CategoryItem>>(
      `${CATEGORY_BASE_URL}/internal/categories/`,
      {
        params,
      }
    );
  },
  getSubjectCategoryList: (params: SubjectCategoryParams) => {
    return request.get<any, CategoryItem[]>(`${CATEGORY_BASE_URL}/product/categories/`, {
      params,
    });
  },
  addCategory: (data: any) => {
    return request.post(`${CATEGORY_BASE_URL}/product/categories/`, data, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  updateCategory: (data: Partial<CategoryFormData>) => {
    return request.put(`${CATEGORY_BASE_URL}/internal/categories/${data.id}/`, data);
  },
  deleteCategory: (id: number) => {
    return request.delete(`${CATEGORY_BASE_URL}/internal/categories/${id}/`);
  },
};

export type CategoryListParams = {} & ListQuery;

export type SubjectCategoryParams = {
  /**
   * 是否查询全部分类，parent和all参数使用一个，优先使用all参数
   */
  all?: string;
  /**
   * 是否置顶，1或0
   */
  is_index?: string;
  /**
   * 上级分类ID，不指定则查询parent为空
   */
  parent?: string;
  /**
   * 查询关键字
   */
  search?: string;
  /**
   * 类目id
   */
  subject?: string;
  [property: string]: any;
};

/**
 * @description 分类列表项
 * @param id 分类ID
 * @param image_url 分类图片
 * @param is_index 是否是首页
 * @param name 分类名称
 * @param parent 父级ID
 * */
export interface CategoryItem {
  id: number;
  image_url: string;
  is_index: boolean;
  name: string;
  parent?: string;
  subject?: SubjectType;
  sequence?: number;
}

/**
 * @description 新增分类
 * @param name 分类名称
 * @param parent 父级ID
 * @param image_url 分类图片
 * @param is_index 是否是首页
 */
export interface CategoryFormData {
  id?: number;
  name: string;
  subject: number;
  parent?: number;
  image_url: File | string;
  is_index: boolean;
  is_active: boolean;
  sequence: number;
  brands: number;
}

export default CategoryAPI;
